classes = [
 {"name": "abyss", "age": 25, "grade": "A"},
 {"name": "kin", "age": 23, "grade": "B"},
 {"name": "tian", "age": 24, "grade": "B"},
 {"name": "hun", "age": 26, "grade": "A"},
 {"name": "exe", "age": 21, "grade": "A"}
]
classes1 = classes[:]  # 复制列表
classes2 = classes[:]  # 复制列表


# 使用函数实现排序
# 冒泡算法
def paixu(x):
    while x < len(classes)-1:
        y = x + 1
        while y < len(classes):
            new_dict0 = classes[x]  # 获取列表中的字典
            new_dict1 = classes[y]
            age0 = new_dict0.get('age')  # 获取字典中成员的年龄
            age1 = new_dict1.get('age')
            if age0 > age1:  # 交换顺序
                tmp = classes[y]
                classes[y] = classes[x]
                classes[x] = tmp
            y += 1
        x += 1
        if x == len(classes)-1:
            print('按成员年龄从小到大排序完成\n', classes)
    return None


paixu(0)


# 使用排序函数实现排序
def my_sort(x):
    return x['age']


classes1.sort(key=my_sort)
print(classes1)


# 使用匿名函数实现排序
classes2.sort(key=lambda x: x['age'])
print(classes2)


# 年龄加1
def age_update():
    x = 0
    while x < len(classes):
        age2020 = list(classes[x].values())  # 把年龄取出为列表
        dict2021 = {'age': int(age2020[1])+1}  # 设置新字典
        classes[x].update(dict2021)   # 更新字典
        x += 1
    print('到了2021年的列表\n', classes)
    return classes


age_update()


# 过滤等级为A的同学
f = filter(lambda x: x['grade'] != 'A', classes)
print(list(f))


# 递归构建斐波那契数列
def fibonacci(x, y):
    if x == 0:
        print(x)
        print(y)
    if x + y <1000:
        z = x + y
        x = y
        print(z)
        fibonacci(y, z)
        return z
    if x + y > 1000:
        return '达到上限'


fibonacci(0, 1)
